package de.scs.core;

import javax.swing.JLabel;

import de.scs.constants.BatteryLevel;
import de.scs.ui.InsulinPumpMonitor;

public class Timer implements Runnable
{

  private int            bslMeasureTime;
  private int            hardwareCheckTime;
  private int            seconds;
  private int            chargeSeconds;
  private static boolean stopped;
  private boolean        pause;
  private JLabel         timerLabel;

  public Timer(int bslMeasureTime, int hardwareCheckTime)
  {
    this.bslMeasureTime = bslMeasureTime;
    this.hardwareCheckTime = hardwareCheckTime;
    timerLabel = (JLabel) InsulinPumpMonitor.getMonitorComponents().get("timer");
  }

  @Override
  public void run()
  {
    while (!stopped)
    {
      try
      {
        if (seconds >= 86400)
        {
          seconds = 0;
          HealthStatusController.totalInjectedDoses = 0;
        }
        if (seconds != 0 && seconds % hardwareCheckTime == 0) HealthStatusController.changeState(4);
        if (seconds % bslMeasureTime == 0 && seconds != 0) HealthStatusController.changeState(2);
        if (seconds % 5 == 0 && seconds != 0) HealthStatusController.changeState(5);
        if (chargeSeconds == 120)
        {
          HealthStatusController.currentBatteryLevel = BatteryLevel.HALF;
          HealthStatusController.changeState(3);
          HealthStatusController.changeState(4);
        }
        if (chargeSeconds == 240)
        {
          HealthStatusController.currentBatteryLevel = BatteryLevel.LOW;
          HealthStatusController.changeState(3);
          HealthStatusController.changeState(4);
        }
        if (chargeSeconds == 300)
        {
          HealthStatusController.currentBatteryLevel = BatteryLevel.VERYLOW;
          HealthStatusController.changeState(3);
          HealthStatusController.changeState(4);
        }
        if (chargeSeconds == 420)
        {
          HealthStatusController.currentBatteryLevel = BatteryLevel.EMPTY;
          HealthStatusController.changeState(3);
          HealthStatusController.changeState(4);
        }
        Thread.sleep(1000);
      }
      catch (InterruptedException ignore)
      {
      }
      if (!pause)
      {
        chargeSeconds++;
        setTime(seconds++);
      }
    }
  }

  private void setTime(long timeValue)
  {
    long seconds = timeValue % 60;
    timeValue /= 60;
    long minutes = timeValue % 60;
    timeValue /= 60;
    long hours = timeValue % 24;
    timerLabel.setText(leadingZero(hours) + ":" + leadingZero(minutes) + ":" + leadingZero(seconds));
  }

  private String leadingZero(long zahl)
  {
    String zahlStr = "0" + Long.toString(zahl);
    return zahlStr.substring(zahlStr.length() - 2);
  }

  public boolean isStopped()
  {
    return stopped;
  }

  public void setStopped(boolean stopped)
  {
    Timer.stopped = stopped;
  }

  public int getChargeSeconds()
  {
    return chargeSeconds;
  }

  public void setChargeSeconds(int chargeSeconds)
  {
    this.chargeSeconds = chargeSeconds;
  }

  public int getTimerSeconds()
  {
    return seconds;
  }

  public void setTimerSeconds(int seconds)
  {
    this.seconds = seconds;
  }

  public boolean isPause()
  {
    return pause;
  }

  public void setPause(boolean pause)
  {
    this.pause = pause;
  }
}
